package com.example.xpvideo.service.serviceImpl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.xpvideo.context.PageContext;
import com.example.xpvideo.entity.*;
import com.example.xpvideo.entity.req.AuditReq;
import com.example.xpvideo.entity.req.PHPCommodityReq;
import com.example.xpvideo.entity.req.RecommendReq;
import com.example.xpvideo.entity.req.UpdateReq;
import com.example.xpvideo.entity.res.LocalGoodsDetailRes;
import com.example.xpvideo.entity.res.LocalGoodsRes;
import com.example.xpvideo.entity.res.PHPCommodityRes;
import com.example.xpvideo.entity.res.PHPLocalMerchantsDetail;
import com.example.xpvideo.enums.ErrCodeEnum;
import com.example.xpvideo.exception.ErrorException;
import com.example.xpvideo.mapper.LocalGoodsMapper;
import com.example.xpvideo.mapper.LsLocalGoodsMapper;
import com.example.xpvideo.service.LocalGoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class LocalGoodsServiceImpl implements LocalGoodsService {
    @Autowired
    private LocalGoodsMapper localGoodsMapper;
    @Autowired
    private LsLocalGoodsMapper lsLocalGoodsMapper;

    @Override
    public ResponseResult<PageResult<List<LocalGoodsRes>>> list(PHPCommodityReq phpCommodityReq) throws ErrorException {

        int page = PageContext.getPage();
        int size = PageContext.getSize();
        String merchantsName = phpCommodityReq.getShopName();
        String commodityName = phpCommodityReq.getName();
        String oneName = phpCommodityReq.getCommodityOneTypeName();
        Integer status = phpCommodityReq.getAuditStatus();
        PageHelper.startPage(page, size);

        List<LocalGoodsRes> list = localGoodsMapper.LocalCommodityList(Wrappers.<LsLocalGoods>query()
                .like(!ObjectUtils.isEmpty(merchantsName),"ls.shop",merchantsName)
                .like(!ObjectUtils.isEmpty(commodityName),"llg.name",commodityName)
                .like(!ObjectUtils.isEmpty(oneName), "lgc.name",oneName));

        PageInfo pageInfo = new PageInfo<>(list);
        //当前页
        int pageNum = pageInfo.getPageNum();
        //总页数
        int pages = pageInfo.getPages();
        //当前页展示的数量
        int pageSize = pageInfo.getSize();
        //查询总数
        long count = pageInfo.getTotal();
        PageResult<List<LocalGoodsRes>> listPageResult = new PageResult<>(count, pages, pageSize, pageNum, list);

        return ResponseResult.responseResult(0, listPageResult);

    }

    @Override
    public ResponseResult<LocalGoodsDetailRes> detail(Integer id) throws ErrorException {

        LocalGoodsDetailRes localGoodsDetailRes = localGoodsMapper.LocalCommodityDetail(Wrappers.<LsLocalGoods>query()
                .eq("llg.id",id));

        return ResponseResult.responseResult(0, localGoodsDetailRes);

    }

    @Override
    public ResponseResult<Object> audit(AuditReq auditReq) throws ErrorException{
        Integer id = auditReq.getId();
        Integer status = auditReq.getStatus();
        String remark = auditReq.getRemark();

        LsLocalGoods lsLocalGoods = new LsLocalGoods();
        lsLocalGoods.setId(id);
        lsLocalGoods.setAuditStatus(status);
        lsLocalGoods.setAuditRemark(remark);

        int res = lsLocalGoodsMapper.updateLsLocalGoods(lsLocalGoods);

        if(res > 0){
            return ResponseResult.responseResult(0, "审核成功");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20000);
    }


    @Override
    public ResponseResult<Object> shelf(UpdateReq updateReq) throws ErrorException{
        Integer id = updateReq.getId();
        Integer stage = updateReq.getStage();

        LsLocalGoods lsLocalGoods = new LsLocalGoods();
        lsLocalGoods.setId(id);
        lsLocalGoods.setStatus(stage);

        int res = lsLocalGoodsMapper.updateLsLocalGoods(lsLocalGoods);

        if(res > 0){
            return ResponseResult.responseResult(0, "上下架成功");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20000);
    }
    @Override
    public ResponseResult<Object> delete(UpdateReq updateReq) throws ErrorException{
        Integer id = updateReq.getId();
        Integer stage = updateReq.getStage();

        LsLocalGoods lsLocalGoods = new LsLocalGoods();
        lsLocalGoods.setId(id);
        lsLocalGoods.setDel(stage);

        int res = lsLocalGoodsMapper.updateLsLocalGoods(lsLocalGoods);

        if(res > 0){
            return ResponseResult.responseResult(0, "删除/放入回收站成功");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20000);
    }

    @Override
    public ResponseResult<Object> recommend(RecommendReq recommendReq) throws ErrorException{
        Integer id = recommendReq.getId();
        Integer stage = recommendReq.getStage();
        Integer days = recommendReq.getDays();

        if(stage == 0){
            LsLocalGoods lsLocalGoods = new LsLocalGoods();
            lsLocalGoods.setId(id);
            lsLocalGoods.setIsRecommend(0);
            lsLocalGoods.setRecommendDays(0);

            int res = lsLocalGoodsMapper.updateLsLocalGoods(lsLocalGoods);

            if(res > 0){
                return ResponseResult.responseResult(0, "取消推荐成功");
            }
            throw new ErrorException(ErrCodeEnum.unknown_20000);
        }
        LsLocalGoods lsLocalGoods = new LsLocalGoods();
        lsLocalGoods.setId(id);
        lsLocalGoods.setIsRecommend(1);
        lsLocalGoods.setRecommendDays(days);

        int res = lsLocalGoodsMapper.updateLsLocalGoods(lsLocalGoods);
        if(res > 0){
            return ResponseResult.responseResult(0,"推荐成功");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20000);
    }
}
